package pt.mauromad.guias.controller;

import java.util.HashSet;
import java.util.Set;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pt.mauromad.guias.dao.DomainDao;
import pt.mauromad.guias.dao.UserDao;
import pt.mauromad.guias.dao.security.RoleDao;

@Controller
public class RootController extends BaseController {
	@RequestMapping(value = "/")
	public String home() {
		return "redirect:/welcome";
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(Model model) {
		if (userService.get("admin\\pedro.amaral") == null) {
			Set<RoleDao> roles = new HashSet<>();

			roles.add(RoleDao.ROLE_USER);
			roles.add(RoleDao.ROLE_ADMIN);

			roleService.save(roles);

			DomainDao domain = new DomainDao();
			domain.setDomain("admin");
			domainService.save(domain);

			roleService.save(roles);

			UserDao user = new UserDao();
			user.setDomain(domain);
			user.setFirstName("Pedro");
			user.setLastName("Amaral");
			user.setUsername("pedro.amaral");
			user.setPassword("teste");

			user.setRoles(roles);

			userService.save(user);
		}
		return "login";
	}

	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public String welcome(Model model) {
		return redirect("/user/%s", getUsername());
	}
}
